package com.csx.governance.traffic.common;

import java.util.*;

/**
 * 复制对象引用类。 复制出一份原始对象，返回复制后的对象，防止对原始对象做改动
 * @author cuisongxu
 * @date 2023/3/18 11:27
 */
public class CopyObjectReference<T> {

    private T copyTarget;

    @SuppressWarnings("unchecked")
    public CopyObjectReference (T target) {
        Objects.requireNonNull(target, "target must not be null");
        if(target instanceof List) {
            List<Object> list = new ArrayList<>((Collection<?>)target);
            this.copyTarget = (T) list;
        }else if(target instanceof Set) {
            Set<Object> set = new HashSet<>((Collection<?>) target);
            this.copyTarget = (T) set;
        }else if(target instanceof Map) {
            Map<Object, Object> map = new HashMap<>((Map<?,?>) target);
            this.copyTarget = (T) map;
        }else if(target instanceof Collection) {
            List<Object> list = new ArrayList<>((Collection<?>)target);
            this.copyTarget = (T) list;
        }
    }

    public T get() {
        return copyTarget;
    }
}
